from flask import request, current_app

def global_before_request():
    """
    每个请求前执行
    """
    method = request.method
    current_app.logger.info('------------ 请求日志 ------------')
    current_app.logger.info(f'请求接口: {request.path}')
    current_app.logger.info(f'请求方式: {method}')
    if method == 'GET':

        current_app.logger.info(f'请求参数: {request.args.to_dict()}')
    else:
        current_app.logger.info(f'请求体: {request.get_json()}')
    current_app.logger.info(f'请求来源IP: {request.remote_addr}')
    current_app.logger.info('--------------------------------')


def global_after_request(response):
    """
    每个请求后执行（无异常）
    """
    current_app.logger.info(f'------------ 响应日志 ------------')
    current_app.logger.info(f'响应状态: {response.status}')
    current_app.logger.info(f'响应体: {response.get_data(as_text=True)}')
    current_app.logger.info('--------------------------------')
    return response


def global_teardown_request(response):
    """
    每个请求后执行（有异常）
    """
    return response
